#include <iostream>
#include <vector>
#include <queue>
using namespace std;
char maze[10][10];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
bool bfs(int x, int y, int m)
{
    queue<pair<int, int>> pre_node;
    pre_node.push(make_pair(x, y));
    while (!pre_node.empty())
    {
        for (size_t i = 0; i < 4; i++)
        {
            x = pre_node.front().first;
            y = pre_node.front().second;
            x = x + dir[i][0]; // 上下左右移动
            y = y + dir[i][1];
            if (x >= 0 && x <= m - 1 && y >= 0 && y <= m - 1 && maze[x][y] == '.') // 判断移动后的点是否合法
            {
                pre_node.push(make_pair(x, y));
                maze[x][y] = 'S';
            }
            else if (maze[x][y] == 'E')
            {
                return true;
            }
        }
        pre_node.pop();
    }
    return false;
}
int main()
{
    int K, m, T, xs, ys;
    cin >> K;
    while (K--)
    {
        cin >> m >> T;
        for (size_t i = 0; i < m; i++)
        {
            for (size_t j = 0; j < m; j++)
            {
                cin >> maze[i][j];
                if (maze[i][j] == 'S')
                {
                    xs = i, ys = j;
                }
            }
        }
        cout << bfs(xs, ys, m) << endl;
    }
}